package com.shopnight.utils;


import com.shopnight.config.WxPayConfig;
import com.wechat.pay.contrib.apache.httpclient.util.AesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Map;

@Component
@Slf4j
public class WxdecryptPayinformUtils  {
    @Autowired
    WxPayConfig wxPayConfig;


    /**
     * 对称解密
     * @param bodyMap
     * @return
     */
    public   String decryptFromResource(Map<String, Object> bodyMap) throws GeneralSecurityException {
        //通知数据
        Map<String, String> resourceMap = (Map) bodyMap.get("resource");
        //数据密文
        String ciphertext = resourceMap.get("ciphertext");
        //随机串
        String nonce = resourceMap.get("nonce");
        //附加数据
        String associatedData = resourceMap.get("associated_data");

      //  log.info("密文 ===> {}", ciphertext);
        AesUtil aesUtil = new AesUtil(wxPayConfig.getApiV3Key().getBytes(StandardCharsets.UTF_8));
        String plainText = aesUtil.decryptToString(
                associatedData.getBytes(StandardCharsets.UTF_8),
                nonce.getBytes(StandardCharsets.UTF_8),
                ciphertext
        );
      //  log.info("明文 ===> {}", plainText);
        return plainText;
    }

}
